package jude.java.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {
	public static final String DEFAULT_FORMAT = "yyyy-MM-dd HH:mm";
	public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
	public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";
	public static final String DEFAULT_DATETIME_FORMAT = "yyyy-M-d HH:mm:ss";
	public static final String ALIGN_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public static final long ONE_DAY = 24 * 60 * 60 * 1000;
	public static final long ONE_WEEK = 7 * ONE_DAY;

	public static final SimpleDateFormat _defDateTimeFmt = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);

	public static final SimpleDateFormat _defDateFmt = new SimpleDateFormat(DEFAULT_DATE_FORMAT);

	public static String toString(Date date, String format) {
		if ((date == null) || (format == null) || (format.length() == 0)) {
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}
	
	public static String toString(Date date) {
		return toString(date, DEFAULT_FORMAT);
	}

	public static Date toDate(String str, String format) {
		if ((str == null) || (str.length() == 0) || (format == null) || (format.length() == 0)) {
			return null;
		}

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		formatter.setLenient(false);
		ParsePosition pos = new ParsePosition(0);
		return formatter.parse(str, pos);
	}

	public static boolean compare(Date date1, Date date2) {
		if (date1 == null && date2 == null) {
			return true;
		}
		if (date1 == null || date2 == null)
			return false;
		else
			return date1.getTime() == date2.getTime();
	}

	public static Date toDate(String str) {
		try {
			if (str.indexOf(':') > 0) {
				return _defDateTimeFmt.parse(str);
			} else {
				return _defDateFmt.parse(str);
			}
		} catch (Exception ex) {
			return null;
		}
	}

	public static String currentDateToString(String format) {
		Date date = new Date();
		return toString(date, format);
	}
	
	public static String curDateStr() {
		return _defDateFmt.format(new Date());
	}
	
	public static String curDateTimeStr() {
		return _defDateTimeFmt.format(new Date());
	}

	public static Date getStartOfDay(Date d) {
		if (d == null) {
			return null;
		}

		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(d);
		setTodayOrigin(cal);

		return cal.getTime();
	}

	private static void setTodayOrigin(GregorianCalendar cal1) {
		cal1.set(Calendar.AM_PM, Calendar.AM);
		cal1.set(Calendar.HOUR, 8);
		cal1.set(Calendar.MINUTE, 0);
		cal1.set(Calendar.SECOND, 0);
		cal1.set(Calendar.MILLISECOND, 0);
	}
	
	public static int getDayOfMonth(Date date){
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		return day;
	}
	
	public static Date getFirstDayOfMonth(Date curDate){
		GregorianCalendar currentDay = new GregorianCalendar();
		currentDay.setTime(curDate);
		/*int today = */currentDay.get(Calendar.DAY_OF_MONTH);
		int month = currentDay.get(Calendar.MONTH);
		int year = currentDay.get(Calendar.YEAR);

		Calendar thisMonth = Calendar.getInstance();
		thisMonth.set(Calendar.MONTH, month);
		thisMonth.set(Calendar.YEAR, year);
		thisMonth.setFirstDayOfWeek(Calendar.SUNDAY);
		thisMonth.set(Calendar.DAY_OF_MONTH, 1);
		thisMonth.set(Calendar.AM_PM, Calendar.AM);
		thisMonth.set(Calendar.HOUR, 0);
		thisMonth.set(Calendar.MINUTE, 0);
		thisMonth.set(Calendar.SECOND, 0);
		thisMonth.set(Calendar.MILLISECOND, 0);
		
		return thisMonth.getTime();
	}
}


